﻿using System.Windows.Controls;
using ComicTool.Model;

namespace ComicTool
{
    public partial class EditPages : ChildWindow
    {
        private Comic comic;

        public EditPages(ComicView comic)
        {
            // Required to initialize variables
            InitializeComponent();
            DataContext = comic;
            this.comic = comic.Comic;
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Page p = pages.SelectedItem as Page;
            if (p != null)
            {
                comic.Pages.Remove(p);
                comic.DeletedPages.Add(p);
            }
        }

        private void Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
        {
            Page p = deletedPages.SelectedItem as Page;
            if (p != null)
            {
                comic.DeletedPages.Remove(p);

                Page p2 = pages.SelectedItem as Page;
                if (p2 != null)
                {
                    int index = comic.Pages.IndexOf(p2);
                    comic.Pages.Insert(index, p);
                }
                else
                {
                    comic.Pages.Add(p);
                }
                pages.SelectedItem = p;
            }
        }

        private void Button_Click_2(object sender, System.Windows.RoutedEventArgs e)
        {
            Close();
        }

        private void Button_Click_3(object sender, System.Windows.RoutedEventArgs e)
        {
            Page p = pages.SelectedItem as Page;
            if (p != null)
            {
                int index = comic.Pages.IndexOf(p);
                if (index > 0)
                {
                    comic.Pages.Remove(p);
                    comic.Pages.Insert(index - 1, p);
                    pages.SelectedItem = p;
                }
            }
        }

        private void Button_Click_4(object sender, System.Windows.RoutedEventArgs e)
        {
            Page p = pages.SelectedItem as Page;
            if (p != null)
            {
                int index = comic.Pages.IndexOf(p);
                if (index < comic.Pages.Count - 1)
                {
                    comic.Pages.Remove(p);
                    comic.Pages.Insert(index + 1, p);
                    pages.SelectedItem = p;
                }
            }
        }
    }
}